package com.example.password.page;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.password.R;
import com.example.password.db.dao.PasswordDao;
import com.example.password.db.database.PasswordDatabase;
import com.example.password.db.entity.Password;
import com.example.password.page.home.PasswordListAdapter;

import java.util.List;

/**
 * 首页
 */
public class HomeFragment extends Fragment {

    private PasswordListAdapter adapter;
    private PasswordDao passwordDao;
    private Button mBtnAdd;
    private ListView mLvPasswordList;

    public HomeFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initViews(view);
        initData();
        return view;
    }

    private void initData() {
        adapter = new PasswordListAdapter(getContext());
        mLvPasswordList.setAdapter(adapter);
        passwordDao = PasswordDatabase.getInstance(getContext()).passwordDao();
        updateData();
    }

    private void initViews(View view) {
        mBtnAdd = view.findViewById(R.id.btn_add);
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCameraOptionsDialog();
            }
        });
        mLvPasswordList = view.findViewById(R.id.lv_password_list);
    }

    public void updateData() {
        List<Password> passwordList = passwordDao.getPasswordAll();
        adapter.refresh(passwordList);
    }

    /* 打开弹窗 */
    private void showCameraOptionsDialog() {
        final Dialog dialog = new Dialog(requireContext());
        dialog.setContentView(R.layout.password_add_dialog);
        EditText mEdName = dialog.findViewById(R.id.ed_name);
        EditText mEdUsername = dialog.findViewById(R.id.ed_username);
        EditText mEdPassword = dialog.findViewById(R.id.ed_password);
        Button mBtnSave = dialog.findViewById(R.id.btn_save);
        Button mBtnReset = dialog.findViewById(R.id.btn_reset);
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "点击了确认", Toast.LENGTH_SHORT).show();
                String name = mEdName.getText().toString();
                String username = mEdUsername.getText().toString();
                String password = mEdPassword.getText().toString();

                Password passwordEntity = new Password();
                passwordEntity.setName(name);
                passwordEntity.setUsername(username);
                passwordEntity.setPassword(password);
                passwordDao.insertPassword(passwordEntity);
                updateData();
                dialog.dismiss();
            }
        });
        mBtnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }
}